﻿using Core.SpellBehaviors;
using System;
using System.Collections.Generic;
using UnityEngine;
using Utils;

namespace Core
{
    public class CoreResources
    {
        public static Dictionary<string, Type> SpellBehaviors { get; private set; }

        public static void Load()
        {
            SpellBehaviors = new Dictionary<string, Type>();
            LoadSpellBehaviors();
        }

        public static ISpellBehavior ParseSpellBehavior(string definition)
        {
            var parsed = new ValueParser(definition);
            var constructor = SpellBehaviors[parsed.TypeName].GetConstructors()[0];
            var parameterInfos = constructor.GetParameters();
            string[] parameters = new string[parameterInfos.Length];
            for (int i = 0; i < parameters.Length; i++)
            {
                parameters[i] = parsed.Args[i];
            }

            return (ISpellBehavior)constructor.Invoke(parameters);
        }

        private static void LoadSpellBehaviors()
        {
            RegisterSpellBehavior(typeof(ChangeHP));
            RegisterSpellBehavior(typeof(AddEffect));
        }

        private static void RegisterSpellBehavior(Type type)
        {
            SpellBehaviors.Add(type.Name, type);
        }
    }
}
